package com.ihd.ihardware.view.tzc.discovery.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.DiscoveryMjFragmentBinding;
import com.ihd.ihardware.pojo.ArticalRes;
import com.ihd.ihardware.pojo.SceneRes;
import com.ihd.ihardware.view.tzc.discovery.viewmodel.DiscoveryViewModel;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.base.BaseFragment;
import cn.wowjoy.commonlibrary.utils.GlideRoundTransform;

public class MJFragment extends BaseFragment<DiscoveryMjFragmentBinding, DiscoveryViewModel> {

    public MJFragment() {
    }

    public static MJFragment newInstance(String param1, String param2) {
        MJFragment fragment = new MJFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        binding.mjRV.setAdapter(viewModel.mCollectAdapter);
        setRx(AppConstans.JXMJ, new BaseConsumer<SceneRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(SceneRes mLiveData) {
                List<String> mBanner = new ArrayList<>();
                List<String> mBannerHint = new ArrayList<>();
                for (SceneRes.DataBean sd : mLiveData.getData()) {
                    mBanner.add(sd.getIconUrl());
                    mBannerHint.add("");
                }
                binding.bannerGuideContent.setData(mBanner, mBannerHint);
                binding.bannerGuideContent.setAdapter(new BGABanner.Adapter<ImageView, String>() {
                    @Override
                    public void fillBannerItem(BGABanner banner, ImageView itemView, @Nullable String model, int position) {
                        Glide.with(MJFragment.this)
                                .load(model)
                                .apply(new RequestOptions().transform(new GlideRoundTransform(5)))
                                .into(itemView);

                        itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if ("1".equals(mLiveData.getData().get(position).getType())) {
                                    Intent i = new Intent(v.getContext(), BaseWebActivity.class);
                                    i.putExtra("url", mLiveData.getData().get(position).getUrl());
                                    v.getContext().startActivity(i);
                                } else {
                                    Intent i = new Intent(v.getContext(), WebActivity.class);
                                    i.putExtra("title", mLiveData.getData().get(position).getCategoryName() + "秘籍-详情");
                                    i.putExtra("url", mLiveData.getData().get(position).getId());
                                    i.putExtra("count", mLiveData.getData().get(position).getCommentNum());
                                    i.putExtra("collectState", mLiveData.getData().get(position).isCollectState());
                                    v.getContext().startActivity(i);
                                }
                            }
                        });
                    }
                });
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        setRx(AppConstans.ARTS, new BaseConsumer<ArticalRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(ArticalRes mLiveData) {
                viewModel.setCollectList(mLiveData.getData().getList());
                binding.mjRV.setHasFixedSize(true);
                binding.mjRV.setNestedScrollingEnabled(false);
            }

            @Override
            protected void onError(Throwable error) {
            }
        });


        binding.ysLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), CategoryActivity.class);
                i.putExtra("arts", "饮食");
                i.putExtra("artsId", "1001");
                startActivity(i);
            }
        });

        binding.ydLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), CategoryActivity.class);
                i.putExtra("arts", "运动");
                i.putExtra("artsId", "1002");
                startActivity(i);
            }
        });

        binding.jfLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), CategoryActivity.class);
                i.putExtra("arts", "减肥");
                i.putExtra("artsId", "1003");
                startActivity(i);
            }
        });

        binding.jkLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), CategoryActivity.class);
                i.putExtra("arts", "健康");
                i.putExtra("artsId", "1004");
                startActivity(i);
            }
        });

        binding.myLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), CategoryActivity.class);
                i.putExtra("arts", "母婴");
                i.putExtra("artsId", "1005");
                startActivity(i);
            }
        });
    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        viewModel.getJXMJList();
        viewModel.getCollectList(1, 20, "");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.discovery_mj_fragment;
    }

    @Override
    protected Class getViewModelClass() {
        return DiscoveryViewModel.class;
    }
}
